﻿/**
* Copyright (c) 2021 Oray Inc. All rights reserverd.
*
* This document may not be reproduced, stored
* in a retrieval system, or transmitted, in any form, or by any means,
* electronic, mechanical, photocopying, recording, or otherwise,
* without the prior consent of Oray Inc.
*
*
* \file HighResTime.h
* \author panzhiyi
* \date 2021-4-23
* \brief 提供Windows下高精度时钟相关功能，其他平台请有需要的同事帮忙添加
*
*/

#ifndef __HIGH_RES_TIME_H__
#define __HIGH_RES_TIME_H__

#ifdef _WIN32
#include <windows.h>
#else
#include "time/gettimeofday.h"
#endif

class CHighResTimer
{
public:
    CHighResTimer();
    virtual ~CHighResTimer();

    void Start();

    /**
     @brief 停止计时并返回毫秒数
     @return 毫秒数
     */
    unsigned int Stop();

    /**
     @return 毫秒数
     */
    unsigned int GetValue();

private:
#ifdef _WIN32
    LARGE_INTEGER m_liFrequency;
    LARGE_INTEGER m_liStartCount;
#else
    timeval m_tmStart = { 0 };
#endif
    unsigned int m_nTimeMs;
};

class CSystemTimeResModifier
{
public:
    /**
     @remarks 你的电脑可能已经被其他程序设置成了1ms，
              但是请注意从Win10 2002开始，其他程序设置的该数字只对设置进程本身生效
     @param nPeriodInMs 期望的时钟精度，如果系统不支持，内部会自动提高该值
     */
    CSystemTimeResModifier(unsigned int nPeriodInMs = 1);
    ~CSystemTimeResModifier();

private:
    unsigned int m_nPeriodInMs;
};

extern void HighResSleep(unsigned int milliseconds);

#endif